import permision from "@/permission.js";

// #ifdef APP
import {
	registerRequestPermissionTipsListener,
	unregisterRequestPermissionTipsListener,
	setRequestPermissionTips,
} from "@/uni_modules/uni-registerRequestPermissionTips";
// #endif


const permissionList = {
	"android.permission.CAMERA": "相机",
	"android.permission.RECORD_AUDIO": "录音",
	"android.permission.RECEIVE_BOOT_COMPLETED": "开机自启动服务",
	"android.permission.SYSTEM_ALERT_WINDOW": "悬浮窗能显示在其他app上方",
	"android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS": "忽略电池优化",
	"android.permission.WAKE_LOCK": "休眠唤醒",
	"android.permission.DISABLE_KEYGUARD": "允许程序禁用键盘锁",
	"android.permission.ACCESS_FINE_LOCATION": "地址",
	"android.permission.POST_NOTIFICATIONS": '通知权限'
};

var PermissionTips = {
	"android.permission.CAMERA": '<h4 style="font-size:40px;">正在使用相机权限</h4><font color=#cccccc>相机权限</font>',
	"android.permission.READ_PHONE_STATE": '<h4 style="font-size:40px;">正在读取网络状态权限</h4><font color=#cccccc>通讯录权限不会获取任何信息，请注意通讯录权限不会获取任何信息，请注意通讯录权限不会获取任何信息，请注意</font>',
};

// 获取相机使用权限
export async function cameraPermissiong() {
	let status = permision.isIOS ? await permision.requestIOS('camera') :
		await permision.requestAndroid('android.permission.CAMERA');
	return status;
}

// 获取录音使用权限
export async function audioPermissiong() {
	let status = permision.isIOS ? await permision.requestIOS('audio') :
		await permision.requestAndroid('android.permission.RECORD_AUDIO');
	return status;
}

// 获取地理位置权限
export async function locationPermissiong() {
	let status = permision.isIOS ? await permision.requestIOS('location') :
		await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
	return status;
}

// 获取通知权限
export async function notificationPermissiong() {
	let status = permision.isIOS ? await permision.requestIOS('notification') :
		await permision.requestAndroid('android.permission.POST_NOTIFICATIONS');
	return status;
}

// 获取通知权限
export async function alertPermissiong() {
	let status = permision.isIOS ? await permision.requestIOS('alert') :
		await permision.requestAndroid('android.permission.SYSTEM_ALERT_WINDOW');
	return status;
}

export function commonLisenerPermissiong() {
	var brand = uni.getSystemInfoSync().deviceBrand;
	setRequestPermissionTips(PermissionTips);
	registerRequestPermissionTipsListener({
		onRequest: (e) => {
			console.log(JSON.stringify(e));
		},
		onConfirm: (e) => {
			console.log(e);
		},
		onComplete: (e) => {
			// let permission = Object.keys(e)[0];
			// if (e[permission] == "denied") {
			// 	uni.showModal({
			// 		content: permissionList[permission] + "权限已拒绝",
			// 		confirmText: "确定",
			// 		showCancel: false,
			// 		success: function(res) {},
			// 	});
			// }
			// 华为手机在权限禁止之后，再次申请权限不会出现权限申请框。此时应该引导用户去系统设置开启此权限，不应该频繁申请。
			if (brand.toLowerCase() == "huawei") {
				var tips = {};
				var hasDeniedPermission = false;
				for (var k in PermissionTips) {
					if (e[k] != "denied") {
						tips[k] = PermissionTips[k];
					} else {
						hasDeniedPermission = true;
					}
				}
				console.log(tips);
				setRequestPermissionTips(tips); // 更新弹框提醒，防止华为手机不出现权限申请框时权限提醒框闪烁的情况
				if (hasDeniedPermission) {
					uni.showModal({
						content: "权限已经被拒绝，请前往设置中开启",
					});
				}
			}
		},
	});
}